<?
function getHtmlDocList($type, $idValue, $cmdL='') {
   global $objSess, $objDB;
   $docType = $type;
   require OVF_FS_WEB_ROOT.'/app/_inc/inc.setDocParams.php';
   $msgErr = '';
   $sHtml = '';
   if (reqVar('azione')=='setOpenFolder') {
      $sFoldX = str_replace('/',chr(0),reqVar('folder_path'));
      $objSess->aSV['docFolder'][$type][$sFoldX] = 'open';
   }
   if (reqVar('azione')=='setCloseFolder') {
      $sFoldX = str_replace('/',chr(0),reqVar('folder_path'));
      $objSess->aSV['docFolder'][$type][$sFoldX] = 'close';
   }
   if (reqVar('azione')=='creaDocFolder') {
      $find =reqVar('folder_index');
      if (reqVar('new_folder_'.$find)=='') {
         $msgErr .= "missing folder name<br>";
      }
      if (preg_match("/[^A-Za-z0-9_\\-\\.]/",reqVar('new_folder_'.$find))) {
         $msgErr .= "folder name contains not allowed characters<br>";
      }
      $newPath = reqVar('curr_folder_'.$find).'/'.reqVarTrim('new_folder_'.$find);
      //$msgErr .= ' - create '.$newPath;
      if ($msgErr == '') {
         $idValX = ($newPath[0]=='G')?'0':$idValue;
         $sSql = "INSERT IGNORE INTO $tab SET $idName = ".intval($idValX)
            .", folder = '".addslashes($newPath)."' ";
         $objDB->exec($sSql,'selYYY');
         $_REQUEST['new_folder_'.$find] = '';
         $msgErr = 'created folder';
      }
   }
   if (reqVar('azione')=='renaDocFolder') {
      $find =reqVar('folder_index');
      if (reqVar('new_folder_'.$find)=='') {
         $msgErr .= "missing folder name<br>";
      }
      if (preg_match("/[^A-Za-z0-9_\\-\\.]/",reqVar('new_folder_'.$find))) {
         $msgErr .= "folder name contains not allowed characters<br>";
      }
      if ($msgErr == '') {
         $oldPath = reqVar('curr_folder_'.$find);
         $aX = explode('/',$oldPath);
         $aX[count($aX)-1] = reqVar('new_folder_'.$find);
         $newPath = implode('/',$aX);
         $posX = strlen($oldPath)+1;
         $sSql = "UPDATE $tab SET folder = CONCAT('".addslashes($newPath)."',SUBSTRING(folder,$posX)) "
            ." WHERE folder like '".addslashes($oldPath)."%' "
            ." AND $idName IN (0,".intval($idValue).") ";
         $objDB->exec($sSql,'selYYY');
         $_REQUEST['new_folder_'.$find] = '';
         $msgErr = 'folder renamed ';
      }
   }
   if (reqVar('azione')=='deleDocFolder') {
      $msgErr .= "";
      $folderPath = reqVar('curr_folder_'.reqVar('folder_index'));
      if ($folderPath=='') {
         $msgErr .= "no folder ".reqVar('folder_index')." ?<br>";
      }
      if ($folderPath[0]=='G') {
         $sSql = "SELECT id_doc FROM $tab WHERE folder = '".addslashes($folderPath)."' "
            ." AND file_name <> ''";
      } else {
         $sSql = "SELECT id_doc FROM $tab WHERE $idName = ".intval($idValue)
            ." AND folder = '".addslashes($folderPath)."' "
            ." AND file_name <> ''";
      }
      $objDB->query($sSql,'selYYY');
      $aX = $objDB->fetch('selYYY');
      if ($aX) {
         $msgErr .= "not empty folder $folderPath<br>";
      } 
      if ($folderPath[0]=='G') {
         $sSql = "SELECT id_doc FROM $tab WHERE folder <> '".addslashes($folderPath)."' "
         ." AND folder like '".addslashes($folderPath)."%'";
      } else {
         $sSql = "SELECT id_doc FROM $tab WHERE $idName = ".intval($idValue)
            ." AND folder <> '".addslashes($folderPath)."' "
         ." AND folder like '".addslashes($folderPath)."%'";
      }
      $objDB->query($sSql,'selYYY');
      $aX = $objDB->fetch('selYYY');
      if ($aX) {
         $msgErr .= "existing sub-folder for $folderPath<br>";
      } 
      //$msgErr .= ' - delete '.$folderPath;
      if ($msgErr == '') {
         $sSql = "DELETE FROM $tab WHERE $idName IN (0,".intval($idValue).") "
            ." AND folder = '".addslashes($folderPath)."' "
            ." AND file_name = ''";
         $objDB->exec($sSql,'selYYY');
         $msgErr = 'folder deleted ';
      }
   }
   $sHtml .= "\n"; 
   //if ($msgErr!='') {
      $sHtml .= "     
   <tr>
      <td class='err1' align='center' colspan='20'><a name='titDoc'></a>$msgErr</td>
   </tr>\n"
      ;   
  // }
   $cmdR1 = '';
   if (true) {//$objSess->getGrant($docGrant) == 'W') {
      $cmdR1 .= "<a class=\"cmd1\" href=\"".OVF_URL_ROOT."/app/_com/entDocIns.php?type=$type&id=$idValue&folder=S&&xsn=".XSN."\">nuovo documento</a>";
   }
   $cmdR1 .= cmdTorna(); 
   
   $sSql = "SELECT d.id_doc, file_name, folder, d.description, d.uploading_time, d.uploading_user, d.note, d.$idName "
      .", u.nome, u.cognome FROM $tab d "
      ." LEFT JOIN ovf_utenti u ON u.id_utente = d.uploading_user"
      ." WHERE d.$idName IN (0,".intval($idValue).") ORDER BY folder ";
   $objDB->query($sSql,'selYYY');
   $aDoc = array();
   while($acRec = $objDB->fetch('selYYY')) {
      if ($acRec['folder']=='') $acRec['folder'] = 'S';
      //check parents folder
      $aX = explode('/',$acRec['folder']);
      $kDoc1 = '';
      foreach($aX as $kX => $sF) {
         $kDoc1 .= $sF;
         if (!key_exists($kDoc1,$aDoc)) {
            $aDoc[$kDoc1] = array();
         }
         $kDoc1 .= chr(0);
      }      
      $kDoc1 = str_replace('/',chr(0),$acRec['folder']);
      $kDoc2 = $acRec['file_name'].'/'.$acRec['id_doc'];
      $aDoc[$kDoc1][$kDoc2] = $acRec;
   }
   //return "<tr><td colspan='20'><pre>".print_r($aDoc,1)."</pre></td></tr>";
   //dbg_($aDoc);
   ksort($aDoc);
   $aFolderType = array('S'=>'specifica','G'=>'globale');
   $folderIndex = 0;
   reqDef('curr_folder_'.$folderIndex,'S');
   $clx = 'txt1';
   $sHtml .= "
   <script>   
   function submDocFolder(act,folderIndex,anchor) {
      document.frm1.folder_index.value = folderIndex;
      document.frm1.azione.value = act;
      document.frm1.scrollVert.value = window.pageYOffset;
      document.frm1.submit();
   }
   function setOpenFolder(folderPath,anchor) {
      document.frm1.folder_path.value = folderPath;
      document.frm1.scrollVert.value = window.pageYOffset;
      document.frm1.azione.value = 'setOpenFolder';
      document.frm1.submit();
   }
   function setCloseFolder(folderPath,anchor) {
      document.frm1.folder_path.value = folderPath;
      document.frm1.scrollVert.value = window.pageYOffset;
      document.frm1.azione.value = 'setCloseFolder';
      document.frm1.submit();
   }
   </script>";
   if (true) {//$objSess->getGrant($docGrant) == 'W') {
      $sHtml .= "
   <tr>
      <td class='cmd1' align='left' colspan='10'>
         $cmdL
      </td>
      <td class='cmd1' align='right' colspan='10'>
         $cmdR1
      </td>
         </tr>\n";
   }
   //$sHtml .= "<tr><td class='tit2' align='center' colspan='20'>Documenti</td></tr>\n";
      $sHtml .= "<tr>
      <td class='$clx' align='left' colspan='8'>
         <img src='".OVF_URL_ROOT."/app/_img/folder_open.png' border='0' align='left'/> &nbsp;Documenti
      </td>";
   if (true) {//$objSess->getGrant($docGrant) == 'W') {
      $sHtml .= "<td class='$clx' align='center' colspan='1'>"
         ."<a class=\"$clx\" href=\"".OVF_URL_ROOT."/app/_com/entDocIns.php?type=$type&&id=$idValue&folder=".urlencode('S')."&xsn=".XSN."\""
         ." title='nuovo documento'>"
         ."<img src='".OVF_URL_ROOT."/app/_img/document-plus.png' border='0' align='center'></a>"
         ."</td>
      <td class='$clx' align='right' colspan='11'>nuova cartella: "
         .getHtmlText('new_folder_'.$folderIndex,15)
         .getHtmlRadio('curr_folder_'.$folderIndex,$aFolderType)
         .getHtmlHidden('folder_index',$folderIndex)
         .getHtmlHidden('folder_path','')
         .getHtmlHidden('scrollVert',0)
         ."&nbsp;&nbsp;&nbsp;<a class='$clx' href=\"javascript:submDocFolder('creaDocFolder',$folderIndex,'titDoc');\">crea</a>
      </td>";
   } else {
      $sHtml .= "<td class='$clx' align='center' colspan='12'>\n";
   }
   $sHtml .= "</tr>\n";
   //dbg_($aDoc);
   foreach($aDoc as $kDoc1 => $aFile) {
      $aFX = explode(chr(0),$kDoc1);
      $foldView = 'open';
      $fileView = 'yes';
      if (count($aFX)>1) {
         $kDocX = $aFX[0];
         for($k=1;$k<count($aFX);$k++) {
            $kDocX .= chr(0).$aFX[$k];
            if ($foldView == 'close') {
               $foldView = 'no';
               break;
            }
            if (ovfVx($objSess->aSV,'docFolder',$type,$kDocX)!='open') {
               $foldView = 'close';
               $fileView = 'no';
            }
            //dbg_("kDocX=$kDocX  - foldView=$foldView fileView=$fileView");
         }      
         //dbg_("aSV=".ovfVx($objSess->aSV,'docFolder',$type,$kDoc1)." - foldView=$foldView fileView=$fileView - $kDoc1");
         if ($foldView=='open') { 
            getHtmlDocFolderOpen($type,$tab,$idValue,$aFX,$sHtml,$folderIndex,$clx,$docGrant);
         }
         if ($foldView=='close') { 
            getHtmlDocFolderClose($type,$tab,$idValue,$aFX,$sHtml,$folderIndex,$clx,$docGrant);
         }
      }
      if ($fileView=='yes') {
         ksort($aFile);
         foreach($aFile as $kdoc2 => $acRec) {
            if ($acRec['file_name']=='') continue;
            getHtmlDocFile($type,$tab,$idValue,$acRec,count($aFX),$sHtml,$clx,$docGrant);
         }
      }
   }
   $sHtml .= "
  <tr>
      <td class='cmd1' align='right' colspan='20'>
         $cmdR1
      </td>
   </tr>\n";
   if (reqVarInt('scrollVert')!=0) {
      $sHtml .= "<script>window.onload = function () { window.scrollTo(0,".reqVarInt('scrollVert')."); }</script>\n";
   }
   
   return $sHtml;
}

function getHtmlDocFolderClose($type,$tab,$idValue,$aFX,&$sHtml,&$folderIndex,&$clx,$docGrant) {
   global $objSess, $objDB;
   //closed folder
   $kLast = count($aFX)-1;
   $indent = ($kLast*20).'px';
   $folderIndex++;
   $folder = $aFX[$kLast];
   $folderPath = implode('/',$aFX);
   $clx = ($clx=='txt1')?'txt2':'txt1';  
   if ($aFX[0]=='G') $folderIcon = 'gfolder_close.png';
   else $folderIcon = 'folder_close.png';
   $folderPathZ = str_replace("'","\\'",$folderPath);
   $sHtml .= "
   <tr>
      <td class='$clx' align='left' colspan='20' valign='top' style='padding-left:$indent;'>
         <a name='f$folderIndex' href=\"#\" onClick=\"setOpenFolder('$folderPathZ','f$folderIndex');\" class='$clx'>
         <img src='".OVF_URL_ROOT."/app/_img/$folderIcon' border='0' align='left'/> $folder</a>
      </td>\n";
   $sHtml .= "</tr>\n";
   return false;
}

function getHtmlDocFolderOpen($type,$tab,$idValue,$aFX,&$sHtml,&$folderIndex,&$clx,$docGrant) {
   global $objSess, $objDB;
   //closed folder
   $kLast = count($aFX)-1;
   $indent = ($kLast*20).'px';
   $folderIndex++;
   $folder = $aFX[$kLast];
   $folderPath = implode('/',$aFX);
   $clx = ($clx=='txt1')?'txt2':'txt1';
   if ($aFX[0]=='G') $folderIcon = 'gfolder_open.png';
   else $folderIcon = 'folder_open.png';
   $folderPathZ = str_replace("'","\\'",$folderPath);
   $sHtml .= "
   <tr>
      <td class='$clx' align='left' colspan='8' valign='top' style='padding-left:$indent;'>
         <a name='f$folderIndex' href=\"#\" onClick=\"setCloseFolder('$folderPathZ','f$folderIndex');\" class='$clx'>
         <img src='".OVF_URL_ROOT."/app/_img/$folderIcon' border='0' align='left'/> $folder</a>
      </td>\n";
   if (true) { //$objSess->getGrant($docGrant) == 'W') {
      $sHtml .= "   
      <td class='$clx' align='center' colspan='1' valign='top'>
         <a class=\"$clx\" href=\"".OVF_URL_ROOT."/app/_com/entDocIns.php?type=$type&&id=$idValue&folder=".urlencode($folderPath)."&xsn=".XSN."\""
         ." title='nuovo documento'>"
         ."<img src='".OVF_URL_ROOT."/app/_img/document-plus.png' border='0' align='center'></a>
      </td>
      <td class='$clx' align='right' colspan='11'>nuova sotto-cartella: "
         .getHtmlText('new_folder_'.$folderIndex,20)
         .getHtmlHidden('curr_folder_'.$folderIndex,$folderPath)
         ."&nbsp;&nbsp;&nbsp;<a class='$clx' href=\"javascript:submDocFolder('creaDocFolder',$folderIndex,'titDoc');\">crea</a>"
         ."&nbsp;&nbsp;&nbsp;<a class='$clx' href=\"javascript:submDocFolder('renaDocFolder',$folderIndex,'titDoc');\">rinomina</a>"
         ."&nbsp;&nbsp;&nbsp;<a class='$clx' href=\"javascript:submDocFolder('deleDocFolder',$folderIndex,'titDoc');\">elimina</a>
      </td>";
    } else {
      $sHtml .= "   
      <td class='$clx' align='center' colspan='12' valign='top'>&nbsp;</td>";
    }
    $sHtml .= "</tr>\n";
    return false;
}
   
function getHtmlDocFile($type,$tab,$idValue,$acRec,$level,&$sHtml,&$clx,$docGrant) {
   global $objSess, $objDB;
   $indent = (($level)*20).'px';
   $clx = ($clx=='txt1')?'txt2':'txt1';
   $sHtml .= "
   <tr>
      <td class='$clx' align='left' colspan='8' valign='top' style='padding-left:$indent;'>
         <a class='$clx' href='".OVF_URL_ROOT."/app/_com/entDocDL.php?type=$type&table=$tab&iddoc=".$acRec['id_doc']."&xsn=".XSN."' target='_blank' 
            title='download document'>".getHtmlImgDoc($acRec['file_name']).'&nbsp;&nbsp;'.$acRec['file_name']."</a>
      </td>";
   if (true) { //$objSess->getGrant($docGrant) == 'W') {
      $sHtml .= "
      <td class='$clx' align='left' colspan='8' valign='top'>
         <a class='$clx' href='".OVF_URL_ROOT."/app/_com/entDocMod.php?type=$type&table=$tab&iddoc="
         .$acRec['id_doc']."&xsn=".XSN."' title='modify document'><img src='".OVF_URL_ROOT."/app/_img/b_edit.png' border='0' height='12' align='left'>"
         .(($acRec['description'])?$acRec['description']:'(update)')."</a>
         <div style='font-size:7pt;'>".nl2br(trim(substr($acRec['note'],0,255)))."</div>
      </td>
      ";
   } else {
       $sHtml .= "<td class='$clx' align='left' colspan='8' valign='top'>&nbsp;</td>\n";  
   }   
    $sHtml .= "<td class='$clx' align='left' colspan='4' valign='top' style='font-size:7pt;'>uploaded by 
         ".(($acRec['nome']=='')?($acRec['uploading_user'].' - installer'):$acRec['nome'].' '.$acRec['cognome'])
         ."<br>on ".date('d/m/Y H:i:s',strtotime($acRec['uploading_time']))."
      </td></tr>\n";
}

